package net.thxjj.adminServer.controller;

import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.entity.ResponseModel;
import net.thxjj.adminServer.entity.ResultEnum;
import net.thxjj.adminServer.service.IRoleService;
import net.thxjj.adminServer.util.Jurisdiction;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/roleManage")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    /**
     * 角色列表
     * @return
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    @ResponseBody
    public Object list(){

        return ResponseModel.getModel(ResultEnum.SUCCESS,roleService.list());
    }


    /**
     * 获取权限列表
     * @return
     */
    @RequestMapping(value="/qx",method=RequestMethod.POST)
    @ResponseBody
    public Object qx(@RequestBody JSONObject jsonObject){
        String qx = jsonObject.getString("qx");

        if(!jsonObject.containsKey("role_id") || Tools.isEmpty(qx)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        long role_id = jsonObject.getLong("role_id");

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("qx",qx);
        parameterMap.put("role_id",role_id);

        return roleService.getMenu(parameterMap);
    }

    /**
     * 添加角色
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody JSONObject jsonObject){

        String role_name = jsonObject.getString("role_name");

        String role_desc = jsonObject.getString("role_desc");

        if(Tools.isEmpty(role_name)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("role_name",role_name);
        parameterMap.put("role_desc",role_desc);

        return roleService.add(parameterMap,this.getSession());
    }

    /**
     * 编辑角色(名称和描述)
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Object edit(@RequestBody JSONObject jsonObject){


        if(!jsonObject.containsKey("role_id")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        long role_id = jsonObject.getLongValue("role_id");

        String role_name = jsonObject.getString("role_name");

        String role_desc = jsonObject.getString("role_desc");

        if(Tools.isEmpty(role_name)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        ParameterMap parameterMap = new ParameterMap();
        parameterMap.put("role_id",role_id);
        parameterMap.put("role_name",role_name);
        parameterMap.put("role_desc",role_desc);

        return roleService.edit(parameterMap);

    }

    /**
     * 编辑角色权限
     * @return
     */
    @RequestMapping(value="/editRights",method=RequestMethod.POST)
    @ResponseBody
    public Object editRights(@RequestBody JSONObject jsonObject){


        if(!jsonObject.containsKey("role_id")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }



        String qx = jsonObject.getString("qx");
        if(Tools.isEmpty(qx)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        if(!qx.equals("rights")&&!qx.equals("add_qx")&&!qx.equals("del_qx")&&!qx.equals("edit_qx")&&!qx.equals("query_qx")){
            return ResponseModel.getModel(ResultEnum.PARAMETER_VALUE_ERROR,null);
        }


        ParameterMap pam = new ParameterMap();
        pam.put("qx",qx);
        try {
            long role_id = jsonObject.getLongValue("role_id");
            String ad = jsonObject.getString("ids");

            pam.put("role_id",role_id);
            if(!Tools.isEmpty(ad)){
                String[] aa = ad.split(",");
                boolean flag = false;
                for (int i=0;i<aa.length;i++){
                    if(!Tools.isLongNumber(aa[i])){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
                }
                pam.put("ids",jsonObject.getString("ids"));
            }else{
                pam.put("ids","");
            }

            return roleService.edit(pam);
        }catch (Exception e){
            return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
        }
    }



    /**
     * 删除角色
     * @return
     */
    @RequestMapping(value="/del",method=RequestMethod.POST)
    @ResponseBody
    public Object deleteRole(@RequestBody JSONObject jsonObject){

        String role_id = jsonObject.getString("role_id");

        if(Tools.isEmpty(role_id)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
        }

        if(!Tools.isLongNumber(role_id)){
            return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
        }

        return roleService.del(role_id);
    }


}
